package yxy.game.pm2.command.pickedtroops;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.commandertallylevel;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroops;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Collection;

/****************************************
 * @name CMD_PickedTroopsUp
 * @description 兵符槽升级或升阶
 * @author 梁健彬
 * @since 2022-05-19
 ***************************************/
@MessageType(proto = Command.PickedTroopsLevelOrStageUp, classtype = PickedTroopsMessage.PickedTroopsLevelOrStageUpRequest.class)
public class CMD_PickedTroopsLevelOrStageUp extends Command<PickedTroopsMessage.PickedTroopsLevelOrStageUpRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsLevelOrStageUpRequest request) {
        String playerCode = messageSource.getTag();//玩家id
        int camp = request.getCamp();//阵容

        PickedTroopsMessage.PickedTroopsLevelOrStageUpResponse.Builder response = PickedTroopsMessage.PickedTroopsLevelOrStageUpResponse.newBuilder();

        try {
            response.setCamp(camp);

            PickedTroopsModule module = PickedTroopsModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);//不存在就参数错误
                return;
            }

            PickedTroops pts = module.getPts().get(camp);
            if (pts == null) {
                send(messageSource, response, Result.DataError);//参数错误
                return;
            }

            if (!pts.isOpen()) {
                send(messageSource, response, Result.PickedTroopsNeedUnlock);//未解锁
                return;
            }

            commandertallylevel currentCommandertallylevel = DataCenter.getData(pts.getCamp() + "_" + pts.getLevel(), commandertallylevel.class);


            //下一级的数据，如果已经升级满了就返回升级已满
            commandertallylevel nextCommandertallylevel = DataCenter.getData(pts.getCamp() + "_" + (pts.getLevel() + 1), commandertallylevel.class);
            if (nextCommandertallylevel == null) {
                send(messageSource, response, Result.PickedTroopsLevelUpper);//兵符槽等级已满
                return;
            }

            //消耗
            Collection<Currency> costcurs = Currency.create(currentCommandertallylevel.getCosttype(), currentCommandertallylevel.getCostid(), currentCommandertallylevel.getCostnum());
            try {
                new CurrencyHandle().cost(playerCode, CCReason.兵符槽, costcurs);
            } catch (CurrencyNotEnoughException e) {
                send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                return;
            }

            //缓存更改
            //todo 如果是升阶就提升一阶
            if (nextCommandertallylevel.getStar() != pts.getStagelevel()) {
                int stagelevel = pts.getStagelevel() + 1;
                pts.setStagelevel(stagelevel);
            }

            int level = pts.getLevel() + 1;
            pts.setLevel(level);
            pts.setDamageincrease(nextCommandertallylevel.getDamageincrease());
            pts.setDamagedrop(nextCommandertallylevel.getDamagedrop());
            module.set();

            PlayTypeHandle.savePickedTroop(playerCode, PlayTypeModule.get(playerCode));
            new BattleHandle().powerUpdate(playerCode);// 战力更新

            for (PickedTroops value : module.getPts().values()) {
                response.addPts(PickedTroopsHandle.getPickedTroops(playerCode, value));//返回兵符槽全部信息
            }

            send(messageSource, response, Result.Success);

        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsLevelOrStageUpResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}